import {baseUrl} from '@/config/env';
import dayjs from "dayjs";

export function formatUrl(url = "") {
    if (/^http:\/\//.test(url)) return url;
    if (/^https:\/\//.test(url)) return url;
    if (url.startsWith("/")) {
        return url;
    }
    if (url.startsWith(baseUrl)) {
        url = url.replace(baseUrl, "")
    }
    return baseUrl + url;
}

export function htmlGetText(html = '', textLength = 0) {
    let temp = html.replace(/<[^>]+>/g, "")
    if (textLength < 1) return temp
    if (temp.length <= textLength) return temp;
    return temp.substring(0, textLength)
}

export function getFormatImg(userInfo, defVal = "account-fill", avatarKey = "avatar") {
    if (!userInfo) return defVal
    if (!userInfo[avatarKey]) return defVal;
    let avatar = userInfo[avatarKey];

    if ((typeof avatar) == "string") {
        avatar = JSON.parse(avatar)
    }
    if (!avatar) {
        return defVal;
    }
    if (avatar.length < 1) {
        return defVal;
    }
    return formatUrl(avatar[0].value)
}


export function autoInterceptFormatDate(str, nowDate = new Date()) {
    if (!str) return ""
    let newDate = nowDate ? dayjs(nowDate) : dayjs();

    let lastDate = dayjs(str);
    let newMillisecond = newDate.toDate().getTime()
    let lastMillisecond = lastDate.toDate().getTime()
    let millisecond = parseInt((newMillisecond - lastMillisecond) / 1000);
    let m = parseInt(millisecond / 60)
    if (m < 2) {
        return "刚刚";
    }
    if (m < 60) {
        return m + "分钟前"
    }
    let h = parseInt(m / 60)
    if (h < 24) {
        return h + "小时前"
    }
    let d = parseInt(h / 24);
    if (d < 2) return "昨天"
    let year = newDate.year() - lastDate.year()
    if (year === 0) {
        return lastDate.format("MM.DD")
    }

    return lastDate.format("YYYY.MM.DD");
}

export function autoFormDate(str, split = ".", defVal = null, nowDate = new Date()) {
    if (!str) return defVal
    let newDate = nowDate ? dayjs(nowDate) : dayjs();
    let lastDate = dayjs(str);
    if (lastDate.year() !== newDate.year()) {
        return lastDate.format("YYYY" + split + "MM" + split + "DD HH:mm")
    }
    if (lastDate.month() !== newDate.month()) {
        return lastDate.format("MM" + split + "DD HH:mm")
    }
    if (lastDate.date() !== newDate.date()) {
        return lastDate.format("MM" + split + "DD HH:mm")
    }
    return lastDate.format("HH:mm");
}

export function formatDate(str, format = "YYYY-MM-DD HH:mm:SS") {
    if (!str) return ""
    let date = dayjs(str)
    return date.format(format)
}

export function autoToStr(val, key, defVal = null) {
    if (!val) return defVal
    if (!key) {
        try {
            return JSON.parse(val)
        } catch (e) {
            return defVal;
        }
    }
    if (!val[key]) return defVal;
    try {
        return JSON.stringify(val[key])
    } catch (e) {
        return defVal
    }
}

export function autoToJSON(val, key, defVal = null) {
    if (!val) return defVal
    if (key) {
        if (!val[key]) {
            return defVal;
        }
        try {
            return JSON.parse(val[key])
        } catch (e) {
            return defVal;
        }
    }
    try {
        return JSON.parse(val)
    } catch (e) {
        return defVal;
    }

}

const startFormatUilts = {
    autoFormDate,
    autoInterceptFormatDate,
    formatDate,
    autoToStr,
    autoToJSON
};
export default startFormatUilts;